package com.controller;

import com.dao.EnvelopeDao;
import com.dao.InboxDao;
import com.dao.NoteDao;
import com.dao.UserDao;
import com.po.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet({"/quoteEnvelope", "/createQuote", "/quoteNote"})
public class QuoteSystem extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String servletPath = request.getServletPath();
        if ("/quoteEnvelope".equals(servletPath)) {
            doEnvelope(request, response);
        } else if ("/createQuote".equals(servletPath)) {
            doQuote(request, response);
        } else if ("/quoteNote".equals(servletPath)) {
            doSelectNote(request, response);
        }
    }

    private void doSelectNote(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        request.setCharacterEncoding("UTF-8");
        String content = request.getParameter("content");
        //分割字符串
        String user_name = content.substring(0, content.indexOf('的'));
        String note_name = content.substring(user_name.length() + 1, content.length());

        List<Comment> quoteNoteComment = NoteDao.getQuoteNoteComment(user_name, note_name);
        if (quoteNoteComment.isEmpty()) {
            out.print("该信封已被删除，无法查看");
        } else {
            request.setAttribute("quoteNoteComment", quoteNoteComment);
            request.setAttribute("content", content);
            request.setAttribute("note_content", NoteDao.getContent(user_name, note_name));
            request.getRequestDispatcher("/comment/quoteComment.jsp").forward(request, response);
        }
    }

    private void doQuote(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession(false);
        int id = (int) session.getAttribute("id");

        String note_userName = (String) session.getAttribute("note_userName");
        String note_name = (String) session.getAttribute("note_name");

        int envelope_id = Integer.parseInt(request.getParameter("envelope_id"));
        String content = request.getParameter("content");
        String name = request.getParameter("name");
        session.removeAttribute("note_userName");
        session.removeAttribute("note_name");
        if (content.length() >= 1 && name.length() >= 1) {
            NoteDao.createQuoteNote(content, envelope_id, name, note_name + "的" + note_userName);
            InboxDao.inboxTip(id, UserDao.getUser_id(note_userName), "note", note_name);

            response.sendRedirect(request.getContextPath() + "/success.jsp");
        } else {
            out.print("输入有空，请重新输入");
        }
    }

    private void doEnvelope(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        request.setCharacterEncoding("UTF-8");
        String note_name = request.getParameter("note_name");

        int note_id = Integer.parseInt(request.getParameter("note_id"));
        String note_userName = EnvelopeDao.getUserName(note_id);

        HttpSession session = request.getSession(false);
        session.setAttribute("note_name", note_name);
        session.setAttribute("note_userName", note_userName);

        int id = (int) session.getAttribute("id");
        String sql = "select * from envelope where user_id = ?";
        List<Envelope> envelopes = EnvelopeDao.getEnvelope(id, sql);

        if (envelopes.isEmpty()) {
            out.print("对不起你还没有信封，请去前往创建信封");
        } else {
            request.setAttribute("envelopes", envelopes);
            request.getRequestDispatcher("/envelope/choiceEnvelope.jsp").forward(request, response);
        }
    }
}
